package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {

        //准备日期列表数据
        List<LocalDate> localDates = new ArrayList<>();

        //循环插入日期数据
        while (!begin.isAfter(end)){
            localDates.add(begin);
            begin = begin.plusDays(1);
        }
        log.info("日期列表数据：{}",localDates);
        List<Double> turnoverList = new ArrayList<>();
        //1.查询订单表，根据日期范围查询指定时间区间的订单
        localDates.forEach(localDate -> {
            HashMap map = new HashMap();
            map.put("status",Orders.COMPLETED);
            map.put("begin", LocalDateTime.of(localDate, LocalTime.MIN));
            map.put("end", LocalDateTime.of(localDate, LocalTime.MAX));
         Double turnover =   orderMapper.sumByMap(map);
            turnover = turnover == null ? 0.0 : turnover;
         turnoverList.add(turnover);

        });

        return TurnoverReportVO.builder()
                .dateList(StringUtils.join(localDates,";"))
                .turnoverList(StringUtils.join(turnoverList,";"))
                .build();





    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> localDateList = new ArrayList<>();
        while (!begin.isAfter(end)){
            localDateList.add(begin);
            begin = begin.plusDays(1);
        }
        log.info("日期列表数据：{}",localDateList);
        //新增用户数量
        List<Integer> newUserList = new ArrayList<>();
        //新增用户总数量
        List<Integer> totalUserList= new ArrayList<>();
        //查询每日用户新增数据
        localDateList.forEach(localDate -> {
            HashMap hashMap = new HashMap();
            hashMap.put("begin",LocalDateTime.of(localDate,LocalTime.MIN));
            hashMap.put("end",LocalDateTime.of(localDate,LocalTime.MAX));
        Integer user=    userMapper.conutMap(hashMap);
            newUserList.add(user);

            //统计总的新增用户
        hashMap.put("begin",null);
            Integer integer = userMapper.conutMap(hashMap);
            totalUserList.add(integer);
        });


        return UserReportVO.builder()
                .dateList(StringUtils.join(localDateList,";"))
                .newUserList(StringUtils.join(newUserList,";"))
                .totalUserList(StringUtils.join(totalUserList,";"))
                .build();
    }

    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
//获取日期数据
        List<LocalDate> localDateList = new ArrayList<>();
        while (!begin.isAfter(end)){
            localDateList.add(begin);
            begin = begin.plusDays(1);
        }
        log.info("日期列表数据：{}",localDateList);
//每日订单总数 orderCountList
        List<Integer> orderCountList = new ArrayList<>();
//每日有效订单数
        List<Integer> validOrderCountList = new ArrayList<>();

        Integer totalOrderCount = 0;
        Integer validOrderCount = 0;
        for (LocalDate localDate : localDateList) {
            HashMap map = new HashMap();
            map.put("begin",LocalDateTime.of(localDate,LocalTime.MIN));
            map.put("end",LocalDateTime.of(localDate,LocalTime.MAX));
            Integer totalOrder = orderMapper.countByMap(map);
            orderCountList.add(totalOrder);
            map.put("status",Orders.COMPLETED);
            Integer validOrder =    orderMapper.countByMap(map);
            validOrderCountList.add(validOrder);
//获取订单总数
            totalOrderCount+=totalOrder;
        //获取有效订单数
            validOrderCount+=validOrder;
        }
        //计算完成率
        Double orderCompletionRate =0.0;
        if (totalOrderCount !=0){
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount.doubleValue();
        }
        //获取对象封装并返回
   return OrderReportVO.builder()
           .dateList(StringUtils.join(localDateList,";"))
           .orderCountList(StringUtils.join(orderCountList,";"))
           .validOrderCountList(StringUtils.join(validOrderCountList,";"))
           .totalOrderCount(totalOrderCount)
           .validOrderCount(validOrderCount)
           .orderCompletionRate(orderCompletionRate)
           .build();


    }

    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {

        //获取nameList
        List<String> nameList = new ArrayList<>();
        //获取numberList
        List<Integer> numberList = new ArrayList<>();
        //查询订单明细表 order_detail表 +order表 条件是状态为已完成的，还下单时间根据日期查询
        HashMap hashMap = new HashMap();
        hashMap.put("status",Orders.COMPLETED);
        hashMap.put("begin",LocalDateTime.of(begin,LocalTime.MIN));
        hashMap.put("end",LocalDateTime.of(end,LocalTime.MAX));
     List<Map> mapList=   orderMapper.sumTop10(begin,end);
     for (Map map : mapList) {
         String name = (String) map.get("name");
         Integer number = (Integer) map.get("number");
         nameList.add(name);
         numberList.add(number);
     }

     //封装对象并返回
return SalesTop10ReportVO.builder()
        .nameList(StringUtils.join(nameList,";"))
        .numberList(StringUtils.join(numberList,";"))
        .build();

    }
}

